package com.aestheticshare.main;

import android.annotation.TargetApi;
import android.app.Activity;
import android.app.NotificationManager;
import android.app.SearchManager;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Build;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.util.Log;

import com.actionbarsherlock.app.SherlockFragmentActivity;
import com.actionbarsherlock.view.Menu;
import com.actionbarsherlock.view.MenuItem;
import com.actionbarsherlock.widget.SearchView;
import com.aestheticshare.main.introduction.IntroductionActivity_;
import com.androidquery.AQuery;
import com.google.analytics.tracking.android.EasyTracker;
import com.google.android.gcm.GCMRegistrar;
import com.googlecode.androidannotations.annotations.AfterViews;
import com.googlecode.androidannotations.annotations.EActivity;
import com.googlecode.androidannotations.annotations.OnActivityResult;
import com.googlecode.androidannotations.annotations.OptionsItem;
import com.googlecode.androidannotations.annotations.SystemService;

@EActivity(R.layout.activity_main)
public class MainActivity extends SherlockFragmentActivity{

	@SystemService NotificationManager notificationManager;
	@SystemService SearchManager searchManager;

	SharedPreferences settings;
	AQuery aq = new AQuery(this);
	
	String userID;
	
	@AfterViews
	void initialize(){
        /*
         * settings
         */
		settings = PreferenceManager.getDefaultSharedPreferences(this);
		
		
		/*
		 * Google Cloud Messaging
		 */
		try{
			GCMRegistrar.checkDevice(this);
			GCMRegistrar.checkManifest(this);
			final String regID = GCMRegistrar.getRegistrationId(this);
			if (regID.equals("")) {
			  GCMRegistrar.register(this, GCMIntentService.SENDER_ID);
			} else {
			  Log.v(GCMIntentService.TAG, "Already registered. Registration id:" + regID);
			}
		}catch(Exception e){
			e.printStackTrace();
		}
		
		
		/*
		 * google analytics
		 */
		EasyTracker.getInstance().setContext(this);        
        
		
		/*
		 * introduction
		 */
		if(settings.getInt(Utils.KEY_INTRO_PAGE_POSITION, 0) != Integer.MAX_VALUE){
			startActivity(new Intent(MainActivity.this, IntroductionActivity_.class).putExtra(Utils.KEY_INTRO_PAGE_POSITION, 0));
			overridePendingTransition(android.R.anim.fade_in, android.R.anim.fade_out);
		}		
	}
	
	void handleIntent(Intent intent){
		Utils.log();
        if (Intent.ACTION_SEARCH.equals(intent.getAction())) {

        } else if (Intent.ACTION_VIEW.equals(intent.getAction())) {
        	
        }
	}
	
	@OptionsItem(R.id.menu_help)
	void onMenuHelp() {
		startActivity(new Intent(MainActivity.this, IntroductionActivity_.class).putExtra(Utils.KEY_INTRO_PAGE_POSITION, 0));
	}

	@OptionsItem(R.id.menu_settings)
	void onMenuSettings() {
	}

	@OptionsItem(R.id.menu_about)
	void onMenuAbout() {
        PopupWebActivity.popup(this, Utils.URL_BASE_SERVER + "test.html", 0);
	}

	@OnActivityResult(Utils.REQUEST_CODE_AUTH)
	void onAuthResult(int resultCode, Intent data) {
		if(resultCode==Activity.RESULT_OK && data!=null){
			userID = data.getStringExtra("id");
			getSupportActionBar().setSubtitle(data.getStringExtra("name"));
		}
	}
	
	@Override
    protected void onCreate(Bundle savedInstanceState) {
    	setTheme(R.style.Theme_Sherlock);
    	super.onCreate(savedInstanceState);
        handleIntent(getIntent());
    }
	
	@TargetApi(Build.VERSION_CODES.HONEYCOMB)
	@Override
    public boolean onCreateOptionsMenu(Menu menu) {
        SearchView searchView = new SearchView(getSupportActionBar().getThemedContext());
		searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));
        searchView.setIconifiedByDefault(false);
        searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener(){
			@Override
			public boolean onQueryTextSubmit(String query) {
				return false;
			}

			@Override
			public boolean onQueryTextChange(String newText) {
				return false;
			}
        	
        });
        searchView.setOnSuggestionListener(new SearchView.OnSuggestionListener() {
			@Override
			public boolean onSuggestionSelect(int position) {
				return false;
			}
			
			@Override
			public boolean onSuggestionClick(int position) {
				return false;
			}
		});

        
        menu.add(getResources().getString(R.string.menu_search))
        .setActionView(searchView)
        .setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS);
		
		getSupportMenuInflater().inflate(R.menu.activity_main, menu);
		
		
		return true;
	}

	
    @Override
    protected void onNewIntent(Intent intent) {
        handleIntent(intent);
    }
	
	@Override
	protected void onStart() {
		super.onStart();
		EasyTracker.getInstance().activityStart(this);
	}

	@Override
	protected void onStop() {
		super.onStop();
		EasyTracker.getInstance().activityStop(this);
	}	
	
}

